package com.cnc.service.Impl;

import com.cnc.dao.CourseMapper;
import com.cnc.dao.UserCourseMapper;
import com.cnc.dao.UserMapper;
import com.cnc.pojo.Course;
import com.cnc.pojo.UserCourse;
import com.cnc.service.UserCourseService;
import com.cnc.utils.JsonUtil;
import com.cnc.utils.PageUtil;
import com.cnc.utils.SqlUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class UserCourseServiceImpl implements UserCourseService {
    @Override
    public String getSelectList(String currentPage, String pageSize, String search) {
        PageUtil pu = new PageUtil(pageSize, currentPage);
        SqlSession sqlSession = SqlUtil.getSqlSession();
        UserCourseMapper mapper = sqlSession.getMapper(UserCourseMapper.class);
        UserMapper mapper1 = sqlSession.getMapper(UserMapper.class);
        CourseMapper mapper2 = sqlSession.getMapper(CourseMapper.class);
        List<UserCourse> list = mapper.getSelectList(pu, search);
        list.forEach(i->{
            i.setUser(mapper1.getUserById(i.getUid()));
            i.setCourse(mapper2.getCourseById(i.getCid()));
        });
        pu.setResult(list);
        pu.setCountRows(mapper.getCountRows(search));
        sqlSession.close();
        return JsonUtil.objectToJson(pu);
    }

    @Override
    public String delAllSelect(String data) {
        ArrayList<Integer> list = (ArrayList<Integer>) JsonUtil.JsonToObject(data, Object.class);
        SqlSession sqlSession = SqlUtil.getSqlSession();
        UserCourseMapper mapper = sqlSession.getMapper(UserCourseMapper.class);
        list.forEach(i->mapper.delUserCourseById(i));
        sqlSession.commit();
        return "删除成功！";
    }

    @Override
    public String update(String id, String cid) {
        SqlSession sqlSession = SqlUtil.getSqlSession();
        UserCourseMapper mapper = sqlSession.getMapper(UserCourseMapper.class);
        int i = mapper.update(id, cid);
        sqlSession.commit();
        sqlSession.close();
        if(i!=0){
            return "修改成功！";
        }else {
            return "修改失败！";
        }
    }

    @Override
    public boolean haveBuy(String uid, String cid) {
        SqlSession sqlSession = SqlUtil.getSqlSession();
        UserCourseMapper mapper = sqlSession.getMapper(UserCourseMapper.class);
        int i = mapper.haveBuy(uid, cid);
        sqlSession.close();
        if(i!=0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean add(String uid, String cid) {
        SqlSession sqlSession = SqlUtil.getSqlSession();
        UserCourseMapper mapper = sqlSession.getMapper(UserCourseMapper.class);
        int i = mapper.add(uid, cid);
        sqlSession.commit();
        sqlSession.close();
        if(i!=0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public String getMyCourse(String uid) {
        SqlSession sqlSession = SqlUtil.getSqlSession();
        UserCourseMapper mapper = sqlSession.getMapper(UserCourseMapper.class);
        List<Course> list=mapper.getMyCourse(uid);
        return JsonUtil.objectToJson(list);
    }
}
